package com.wandian.cqcsk.mvvm.mine.mywallet.mytxhistory;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.bean.EveryDayConfigBean;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.cqcsk.bean.TXConfigBean;
import com.wandian.cqcsk.bean.TXHistoryBean;
import com.wandian.cqcsk.data.BaseAppApi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description MyTXHistoryViewModel
 **/
public class MyTXHistoryViewModel extends BaseViewModel {
    private BaseAppApi baseAppApi;
    public MutableLiveData<List<TXHistoryBean>> listData = new MutableLiveData<>();
    public MyTXHistoryViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }

    public void getWithdrawalList(int pageNum, int pageSize,BaseQuickAdapter adapter){
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("pageNum",pageNum+"");
        hashMap.put("pageSize",pageSize+"");
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.withdrawalList(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        if(!UIUtils.isEmpty(adapter))adapter.setEnableLoadMore(false);
                        Log.d("withdrawalList",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            try {
                                if(!UIUtils.isEmpty(bean.getData())){
                                    Log.d("crc_rc4key_getVersion", AESUtils.getInstance().decrypt(bean.getData()));
                                    List<TXHistoryBean> listBean = JsonUtil.fromJson(AESUtils.getInstance().decrypt(bean.getData()), new TypeToken<ArrayList<TXHistoryBean>>() {});
                                    if(!UIUtils.isEmpty(listBean) && listBean.size()>0){
                                        if(listBean.size()==pageSize){
                                            if(!UIUtils.isEmpty(adapter))adapter.setEnableLoadMore(true);
                                        }else {
                                            if(!UIUtils.isEmpty(adapter))adapter.loadMoreEnd(true);
                                        }
                                        listData.setValue(listBean);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(!UIUtils.isEmpty(adapter))adapter.setEnableLoadMore(false);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
